import { mainProjectConfig, getChildProjectConfig } from '../config/index';

export function childProjectAll(
  mainKey: string,
  props: Record<string, any> = {},
  container: string = '#subapp-viewport'
) {
  const Apps: any[] = [];
  const Main = mainProjectConfig[mainKey] || null;
  if (!Main) {
    return Apps;
  }

  const childProjectConfig = getChildProjectConfig();
  const subPro = Object.keys(childProjectConfig);
  subPro.map((subProKey) => {
    const subProItem = childProjectConfig[subProKey] || null;
    if (subProItem) {
      Apps.push({
        name: subProKey,
        entry:
          process.env.NODE_ENV === 'production'
            ? `${process.env.MICRO_PUBLIC_PATH}${Main.buildChildParentName}/${subProItem.rootDir}/`
            : `//${subProItem.host}:${subProItem.port}${process.env.MICRO_PUBLIC_PATH}`,
        activeRule: `${process.env.MICRO_PUBLIC_PATH}${subProItem.rootDir}`,
        container, // 子应用挂载的div
        props: {
          routerBase: `${process.env.MICRO_PUBLIC_PATH}${subProItem.rootDir}`,
          ...props,
        },
      });
    }
  });

  return Apps;
}
